package edu.ycxy.poetry.controller;


import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ChengYu;
import edu.ycxy.poetry.service.IChengYuService;
import edu.ycxy.poetry.vo.AddIdiomVo;
import edu.ycxy.poetry.vo.UpdateIdiomVo;
import edu.ycxy.poetry.vo.UpdatePoemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@RestController
@RequestMapping("/idiom")
@Slf4j
public class ChengYuController {

    @Autowired
    private IChengYuService chengYuService;

    //查询所有的成语 , 并对其进行分页处理
    @RequestMapping("/selectList")
    public PageInfo<ChengYu> selectListByIdiom(Integer pageNum){
        System.out.println("------------------------------");
        if (pageNum == null){
            pageNum = 1;
        }
        //对查询出的结果进行分页,每页为20
        PageInfo<ChengYu> chengYuPageInfo = chengYuService.selectListToIdiom(pageNum, 20);
        log.debug("成语的结果集:"+chengYuPageInfo);
        return chengYuPageInfo;
    }

    //根据成语名查询成语的详细信息
    @RequestMapping("/selectOne/{idiomName}")
    public ChengYu selectIdiomByTitle(@PathVariable("idiomName") String idiomName){
        log.debug(idiomName+"的详细信息为:"+chengYuService.selectIdiomByTitle(idiomName));
        return chengYuService.selectIdiomByTitle(idiomName);
    }

    //添加成语
    @RequestMapping("/addIdiom")
    public Integer addIdiom(@RequestBody AddIdiomVo addIdiomVo){
        String s = chengYuService.addIdiom(addIdiomVo);
        if (s.equals("ok")){
            return 1;
        }else {
            return 2;
        }
    }

    //模糊查询
    @RequestMapping("/fuzzy/search/{title}")
    public PageInfo<ChengYu> fuzzySearch(@PathVariable String title,Integer pageNum){
        String name = title.trim();
        if (pageNum == null){
            pageNum = 1;
        }
        PageInfo<ChengYu> listByFuzzy = chengYuService.findListByFuzzy(name, pageNum, 10);
        return listByFuzzy;
    }

    @RequestMapping("/delete/{id}")
    public void deleteIdiom(@PathVariable Integer id){
        log.debug("获取的id" + id);
        chengYuService.deleteIdiom(id);
    }

    @RequestMapping("/findOne/{id}")
    public ChengYu findOneById(@PathVariable Integer id){
        return chengYuService.findOneById(id);
    }

    @RequestMapping("/update")
    public Integer updatePoemById(@RequestBody UpdateIdiomVo updateIdiomVo){
        log.debug("获取到的数据：" + updateIdiomVo);
        Integer integer = chengYuService.updateIdiom(updateIdiomVo);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }
}
